from typing import List

from torch import le


class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        if len(matrix)==0: return matrix
        left = 0
        right = len(matrix[0]) - 1
        top = 0
        bottom = len(matrix)-1
        res = []
        while  left<=right and top<=bottom:
            #右
            if left>right:
                break
            for i in range(left, right+1):
                res.append(matrix[top][i])
            top+=1
            #下
            if top>bottom:
                break
            for i in range(top, bottom+1):
                res.append(matrix[i][right])
            right -= 1
            #左
            if left>right:
                break
            for i in range(right, left-1, -1):
                res.append(matrix[bottom][i])
            bottom -=1
            # 上
            if top>bottom:
                break
            for i in range(bottom, top-1, -1):
                res.append(matrix[i][left])
            left+=1
        return res


if __name__ == '__main__':
    Solution().spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12]])